package com.example.myapplication

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.Color
import android.util.Log
import android.view.View
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageProxy
import com.example.myapplication.MainActivity.Companion.detect
import java.nio.ByteBuffer
import java.util.*


typealias LumaListener = (luma: Double) -> Unit

class LuminosityAnalyzer(private val listener: LumaListener) : ImageAnalysis.Analyzer {

    private var zheView: ZheView? = null
    private var zheView2: ZheView2? = null

    private fun ByteBuffer.toByteArray(): ByteArray {
        rewind()    // Rewind the buffer to zero
        val data = ByteArray(remaining())
        get(data)   // Copy the buffer into a byte array
        return data // Return the byte array
    }

    @SuppressLint("UnsafeExperimentalUsageError")
    override fun analyze(image: ImageProxy) {
        //图片数据处理
        Log.e("${image.format}的宽高", "${image.width}--${image.height}")
        val nv21 = I420Utils.getBytes(image.image, image.width)
        val floatArray = detect.detectyuv(
                nv21,
                image.width,
                image.height
        )
        Log.e("返回的坐标数据", Arrays.toString(floatArray))
        //返回12个值，前6个为左眼的左眼角-瞳孔位置-右眼角，后6个为右眼的右眼角-瞳孔位置-右眼角
        //[48.0, 255.0, 80.0, 241.0, 64.0, 244.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

        //这里使用了YuvImage，接收NV21的数据，得到一个Bitmap
        val bitmap: Bitmap = I420Utils.getBitmapImageFromYUV(nv21, image.width, image.height)

        val bitmap2 = Bitmap.createBitmap(image.width, image.height, Bitmap.Config.ARGB_8888)
        bitmap2.eraseColor(Color.parseColor("#FFFFFF"))

        MainActivity.instance.runOnUiThread {
            //左上角图
            MainActivity.imageView.setImageBitmap(bitmap)
            MainActivity.imageView.translationX = ((image.height - image.width) / 2).toFloat()
            MainActivity.imageView.translationY = ((image.width - image.height) / 2).toFloat()
            if (zheView == null) {
                zheView = ZheView(MainActivity.instance, image.width, image.height)
                MainActivity.parentView.addView(zheView)
            }

            //右上角图
            MainActivity.imageView2.setImageBitmap(bitmap2)
            MainActivity.imageView2.translationX =
                    ((MainActivity.widthPixels - image.height) - (image.width - image.height) / 2).toFloat()
            MainActivity.imageView2.translationY = ((image.width - image.height) / 2).toFloat()
            if (zheView2 == null) {
//                zheView2 = ZheView(MainActivity.instance, image.width, image.height)
                zheView2 = ZheView2(MainActivity.instance, image.width, image.height)
                (zheView2 as View).translationX =
                        (MainActivity.widthPixels - image.height).toFloat()
                MainActivity.parentView.addView(zheView2)
            }

            MainActivity.textView.text = Arrays.toString(floatArray)
            if (floatArray.isNotEmpty()) {
                zheView?.setLine(floatArray)
                zheView2?.setLine(floatArray)
            }
        }

        image.close()
    }
}